"use strict"

var  path = require('path')
var wx = require('../wx/index')
var wechatApi = wx.getWechat()


exports.reply = function* (next){
	var message = this.weixin

	
	if (message.MsgType === 'event') {
		if (message.Event === 'subscribe') {
			if (message.EventKey) {
				console.log('扫二维码进来：'+message.EventKey+ '' +message.ticket)
			}
			this.body ='哈哈，你订阅了这个号'
		}
		else if (message.Event === 'unsubscribe') {
			console.log('无情取关')
			this.body = ''
		}
		else if (message.Event === 'LOCATION') {
			this.body = '您上报的位置是：'+message.Latitude+ '/' + message.Longitude+ '-' +message.Precision
		}
		else if (message.Event === 'CLICK') {
			this.body = '您点击了菜单：'+message.EventKey
		}
		else if (message.Event === 'SCAN') {
			console.log('关注后扫二维码'+message.EventKey+ '' +message.Ticket)

			this.body = '看到你扫了一下哦'
		}
		else if (message.Event === 'VIEW'){
			this.body = '您点击了菜单中的链接：'+message.EventKey
		}
		else if (message.Event === 'sancode_push'){
			console.log(message.ScanCodeInfo.ScanType)
			console.log(message.ScanCodeInfo.ScanResult)

			this.body = '您点击了菜单中：'+message.EventKey
		}
		else if (message.Event === 'scancode_waitmsg'){
			console.log(message.ScanCodeInfo.ScanType)
			console.log(message.ScanCodeInfo.ScanResult)
			this.body = '您点击了菜单中：'+message.EventKey
		}
		else if (message.Event === 'pic_sysphoto'){
			console.log(message.SendPicsInfo.PicList)
			console.log(message.SendPicsInfo.Count)
			this.body = '您点击了菜单中：'+message.EventKey
		}
		else if (message.Event === 'pic_photo_or_album'){
			console.log(message.SendPicsInfo.PicList)
			console.log(message.SendPicsInfo.Count)
			this.body = '您点击了菜单中：'+message.EventKey
		}
		else if (message.Event === 'pic_weixin'){
			console.log(message.SendPicsInfo.PicList)
			console.log(message.SendPicsInfo.Count)
			this.body = '您点击了菜单中：'+message.EventKey
		}
		else if (message.Event === 'location_select'){
			console.log(message.SendLocationInfo.location_X)
			console.log(message.SendLocationInfo.location_Y)
			console.log(message.SendLocationInfo.Scale)
			console.log(message.SendLocationInfo.Label)
			console.log(message.SendLocationInfo.Poiname)
			this.body = '您点击了菜单中：'+message.EventKey
		}
	}else if (message.MsgType === 'text') {
		var content = message.Content;
		var reply = '额，你说的'+message.Content+'太复杂了'

		if (content === '1') {
			reply = '天下第一'
		}
		else if (content === '2') {
			reply = '天下第二'
		}
		else if (content === '3') {
			reply = '天下第三'
		}
		else if (content === '4') {
			reply = [{
				title: '技术改变世界',
				description:'只是个描述而已',
				picUrl: 'http://www.iyi8.com/uploadfile/2014/0717/20140717115123134.jpg',
				url: 'https://github.com/'
			},{
				title: 'Nodejs 开发微信',
				description:'崩溃边缘',
				picUrl: 'http://www.iyi8.com/uploadfile/2014/0717/20140717115123328.jpg',
				url: 'https://nodejs.org'
			}]
		}
		else if (content === '5') {
			var data = yield wechatApi.uploadMaterial('image',path.join(__dirname, '../2.jpg'))

			reply = {
				type: 'image',
				mediaId: data.media_id
			}
		}
		else if (content === '6') {
			var data = yield wechatApi.uploadMaterial('video',path.join(__dirname, '../2.mp4'))

			console.log('#',data)
			reply = {
				type: 'video',
				title: '回复视频',
				description: '打排球玩玩啊',
				mediaId: data.media_id
			}
		}
		else if (content === '7') {
			var data = yield wechatApi.uploadMaterial('image',path.join(__dirname, '../2.jpg'))

			reply = {
				type: 'music',
				title: '回复音乐内容',
				description: 'relax',
				musicUrl: 'http://y.baidu.com/song/338865',
				thumbMediaId: data.media_id
			}
		}
		else if (content === '8') {
			var data = yield wechatApi.uploadMaterial('image',path.join(__dirname, '../2.jpg'),{type:'image'})
			console.log('#',data)
			reply = {
				type: 'image',
				mediaId: data.media_id
			}
		}
		else if (content === '9') {
			var data = yield wechatApi.uploadMaterial('video',path.join(__dirname, '../2.mp4'),{type:'video',description:'{"title":"好山好水好风光","introduction":"桂林山水甲天下"}'})
			console.log('#',data)
			reply = {
				type: 'video',
				title: '回复视频',
				description: '打排球玩玩啊',
				mediaId: data.media_id
			}
		}
		else if (content === '10') {
			var picData = yield wechatApi.uploadMaterial('image',path.join(__dirname, '../2.jpg'),{})
			
			var media = {
				articles:[{
					title:'tututu',
					thumb_media_id: picData.media_id,
					author: 'scott',
					digest: '没有摘要',
					show_cover_pic:1,
					content:'没有内容',
					content_source_url: 'https://baidu.com'
				}]
			}

			data = yield wechatApi.uploadMaterial('news',media,{})
			data = yield wechatApi.fetchMaterial(data.media_id,'news',{})

			console.log(data)

			var items = data.news_item
			var news = []

			items.forEach(function(item){
				news.push({
					title:item.title,
					description:item.digest,
					picUrl:picData.url,
					url:item.url
				})
			})

			reply = news
		}
		else if (content === '11') {
			var counts = yield wechatApi.countMaterial()

			var results = yield [
				wechatApi.batchMaterial({
				type:'image',
				offset:0,
				counts:10
			}),
				wechatApi.batchMaterial({
				type:'video',
				offset:0,
				counts:10
			}),
				wechatApi.batchMaterial({
				type:'vioce',
				offset:0,
				counts:10
			}),
				wechatApi.batchMaterial({
				type:'news',
				offset:0,
				counts:10
			})
			]

			reply = '666'
		}
		//else if (content === '12') {
			//var group = yield wechatApi.createGroup('wechat')
			/*console.log('新分组 wechat')
			console.log(group)*/

			//var groups = yield wechatApi.fetchGroups()

			/*console.log('加了 wechat 后的分组列表')
			console.log(groups)*/

			//var group2 = yield wechatApi.checkGroup(message.FromUserName)
			/*console.log('查看自己的分组')
			console.log (group2)*/
			
			//var result = yield wechatApi.moveGroup(message.FromUserName,101)
			/*console.log('移动到 101')
			console.log (result)*/

			//var groups2 = yield wechatApi.fetchGroups()
			/*console.log('移动后的分组列表')
			console.log (groups2)*/

			//var result2 = yield wechatApi.moveGroup([message.FromUserName],103)
			/*console.log('批量移动到 103')
			console.log (result2)*/

			//var groups3 = yield wechatApi.fetchGroups()
			/*console.log('批量移动后的分组列表')
			console.log (groups3)*/

			//var result3 = yield wechatApi.updateGroup(101,'wechat101')
			/*console.log('101 wechat 改名 wechat101')
			console.log (result3)*/

			//var groups4 = yield wechatApi.fetchGroups()
			/*console.log('改名后的分组列表')
			console.log (groups4)*/

			//var result4 = yield wechatApi.deleteGroup(110)
			/*console.log('删除110分组')
			console.log (result4)*/

			//var groups5 = yield wechatApi.fetchGroups()
			/*console.log('删除110后的分组列表')
			console.log (groups5)*/

			//reply = 'group done!'
		//}
		else if (content === '13') {
			var user = yield wechatApi.batchFetchUsers(message.FromUserName,'en')

			var openIds = [
				{
					openid:message.FromUserName,
					lang: 'en'
				}
			]

			var users = yield wechatApi.batchFetchUsers(openIds)

			reply = JSON.stringify(user)
		}
		else if (content === '14') {
			var userlist = yield wechatApi.listUsers()
			/*console.log(userlist)*/

			reply = userlist.total
		}
		else if (content === '15') {
			/*var mpnews = {
				media_id:'bWfNPiJoAzDethLXqkESbz2Zh8HZ7bR5FM4SM6HGnZ4'
			}*/
			var text = {
				'content':'hello wechat'
			}
			var msgData = yield wechatApi.sendByGroup('text',text,103)

			reply = 'yeah!'
		}
		else if (content === '16') {
			var mpnews = {
				media_id:'bWfNPiJoAzDethLXqkESb_GrcrCRbE2tuYcf50r2kN0'
			}

			/*var text = {
				'content':'hello wechat'
			}*/
			var msgData = yield wechatApi.previewMass('mpnews',mpnews,'oI5HD0jOvrEWrrT7HBkG10CeYVJI')

			reply = 'yeah!'
		}
		else if (content === '17') {
			
			var msgData = yield wechatApi.checkMass('1000000003')

			reply = 'yeah!'
		}
		
		//18,19,20测试不了的

		/*else if (content === '18') {
			var tempQr = {
				expire_seconds:400000,
				action_name:'QR_SCENE',
				action_info:{
					scene:{
						scene_id:123
					}
				}
			}
			var permQr = {
				action_name:'QR_LIMIT_SCENE',
				action_info:{
					scene:{
						scene_id:123
					}
				}
			}
			var permStrQr = {
				action_name:'QR_LIMIT_STR_SCENE',
				action_info:{
					scene:{
						scene_str:'abc'
					}
				}
			}

			var qr1 = yield wechatApi.createQrcode(tempQr)
			var qr2 = yield wechatApi.createQrcode(permQr)
			var qr3 = yield wechatApi.createQrcode(permStrQr)

			

			reply = 'yeah!'
		}
		else if(content === '19'){
			var longUrl = 'http://www.baidu.com/'

			var shortData = yield wechatApi.createShorturl(null,longUrl)

			reply = shortData.short_url
		}
		else if(content === '20'){
			var semanticData = {
				query: '大唐双龙传',
				city: '合肥',
				category: 'movie',
				uid: message.FromUserName
			}
			var _semanticData = yield wechatApi.semantic(semanticData)
			console.log(_semanticData)
			reply = JSON.stringify(_semanticData)
		}*/

		this.body = reply
	}
	yield next
}